package com.sinodata.bsm.cicp.finder.solaris;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * cpu发现程序.
 * 程序返回值:CPU主频,CPUID,CPU类型
 */
public class Cpu extends SolarisFinder {
    private static final Logger logger = Logger.getLogger(Cpu.class);
    private static final String SHELL = "psrinfo -v";

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] cpuRes = null;
        BufferedReader reader = null;
        String strLine = null;
        String ptnStr_en1 = "\\s*Status of.+processor\\s+(\\d+)\\s+.+\\s+.+\\s+.+\\s+.+\\s*";
        String ptnStr_en2 = "\\s*The\\s+(.+)\\s+processor operates at\\s+(\\d+)\\s+MHz,\\s*";
        String ptnStr_zh1 = "\\s*虚拟处理器\\s+(\\d+)\\s+在下列时间的状态.+";
        String ptnStr_zh2 = "\\s*(.+)\\s+处理器以\\s+(\\d+)\\s+MHz\\s+运行,\\s*";
        try {
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            //默认操作系统是英文版本的，使用英文版本的正则表达式匹配
            String ptnStr1 = ptnStr_en1;
            String ptnStr2 = ptnStr_en2;
            boolean flag = true;
            //判断操作系统是否为中文版本，如果是，则改用中文版本的正则表达式去匹配命令输出
            while ((strLine = reader.readLine()) != null) {
                if (flag) {
                    if (strLine.indexOf("虚拟处理器") > -1) {
                        ptnStr1 = ptnStr_zh1;
                        ptnStr2 = ptnStr_zh2;
                    }
                    flag = false;
                }
                Pattern pattern1 = Pattern.compile(ptnStr1);
                Pattern pattern2 = Pattern.compile(ptnStr2);
                Matcher matcher1 = pattern1.matcher(strLine);
                if (matcher1.find()) {//新的cpu开始
                    cpuRes = new String[6];
                    list.add(cpuRes);
                    cpuRes[0] = matcher1.group(1);
                    cpuRes[1] = "CPU" + matcher1.group(1);
                    cpuRes[2] = getHostName(ip) + " CPU " + matcher1.group(1);
                    cpuRes[4] = matcher1.group(1); //cpuId
                    continue;
                } else {
                    Matcher matcher2 = pattern2.matcher(strLine);
                    if (matcher2.find()) {
                        cpuRes[5] = matcher2.group(1); //CPU类型
                        cpuRes[3] = matcher2.group(2); //CPU主频
                    }
                }
            }
            return list;
        } catch (IOException e) {
            logger.error("failed to find Solaris cpu", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }
}
